/*
 * SiFiSy - A File System Simulator
 * Copyright (C) 2010  SevenSoft
 *
 * This file is part of SiFiSy.
 *
 * SiFiSy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * SiFiSy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SiFiSy.  If not, see <http://www.gnu.org/licenses/>.
 */

package sifisy.newgui;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.border.TitledBorder;
import sifisy.core.Model;

/**
 * @author Giuseppe Biolo
 * @version %I%, %G%
 * @createData 17/05/2010
 */
public class NewSimulationWidget extends JDialog {
    private JTextField simulationName;
    private JButton buttonOk;
    private JButton buttonCancel;
    private ProjectPanel projectPanel;

    private void initComponents() {
        // Initialization of environment variables
        simulationName = new JTextField(15);
        buttonOk = new JButton("Ok");
        buttonOk.setEnabled(false);
        buttonCancel = new JButton(Model.translate("cancel"));

        // Addition of Listeners
        simulationName.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (simulationName.getText().isEmpty() ||
                        MainWidget.getSimulationsList().contains(simulationName.getText())) {
                    buttonOk.setEnabled(false);
                }
                else
                    buttonOk.setEnabled(true);
            }
        });
        buttonOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //parent.addNodeToTree(simulationName.getText());
                if (projectPanel != null) {
                    // creo una simulazione all'interno di un progetto
                    MainWidget.createSimulation(simulationName.getText(),
                            projectPanel);
                } else {
                    // creo una simulazione esterna al progetto
                    MainWidget.createSimulation(simulationName.getText());
                }
                (NewSimulationWidget.this).dispose();
            }
        });
        buttonCancel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                (NewSimulationWidget.this).dispose();
            }
        });
    }

    public NewSimulationWidget() {
        this(null);
    }

    public NewSimulationWidget(ProjectPanel parentPanel) {
        super(MainWidget.getMainWidgetFrame(), Model.translate("newSimulation"), true);
        projectPanel = parentPanel;
        initComponents();
        setLayout(new BorderLayout());

        JPanel nameLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        nameLabel.add(new JLabel(Model.translate("name") + ":"));

        JPanel nameText = new JPanel(new FlowLayout(FlowLayout.LEFT));
        nameText.add(simulationName);

        JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        namePanel.setBorder(new TitledBorder(Model.translate("addSimulation")));
        namePanel.add(nameLabel);
        namePanel.add(nameText);

        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttons.add(buttonCancel);
        buttons.add(buttonOk);

        add(namePanel,BorderLayout.NORTH);
        add(buttons,BorderLayout.CENTER);
        setSize(280,140);
        setLocationRelativeTo(parentPanel);
        setVisible(true);
    }
}
